package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.web.dao.WebArticleDao;
import ysq.xyg.web.model.po.WebArticle;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.po.WebArticleType;
import ysq.xyg.web.model.vo.WebArticleTypeVo;
import ysq.xyg.web.model.vo.WebArticleVo;
import ysq.xyg.web.service.WebArticleService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 业务模块-文章service实现
 *
 * @author shengQiang yu
 * @date 2019-4-25 16:53:30
 */
@Service("webArticleService")
public class WebArticleServiceImpl extends BaseServiceImpl<WebArticle> implements WebArticleService {

    @Resource
    private WebArticleDao webArticleDao;

    private WebArticle webArticle;

    @Autowired
    public void setBaseDao(WebArticleDao webArticleDao) {
        this.baseDao = webArticleDao;
    }

    /**
     * 获取下拉框集合
     *
     * @param code code值
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    public ReturnEntity acceptSelect(String code) {
        List<WebArticle> list = acceptList(new WebArticleVo());
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(WebArticle webArticle:list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(webArticle.getId());
            sysDictContent.setName(webArticle.getTitle());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(webArticle.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }
    /**
     * 分页查询
     *
     * @param webArticleVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-25 16:53:30
     */
    public ReturnEntity page(WebArticleVo webArticleVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebArticle> list = acceptList(webArticleVo);
        PageInfo<WebArticle> page=new PageInfo<WebArticle>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webArticleVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-25 16:53:30
     */
    public ReturnEntity addOrUpdate(WebArticleVo webArticleVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webArticleVo.getId())){
            webArticle=new WebArticle(currentUser);
            isUpate=false;
        }else{
            webArticle=selectByPrimaryKey(webArticleVo.getId());
            if(ObjectUtil.isNull(webArticle)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webArticle.setUpdateTime(new Date());
            webArticle.setUpdateUser(currentUser.getId());
        }
        webArticle.setTitle(webArticleVo.getTitle());
        webArticle.setDescription(webArticleVo.getDescription());
        webArticle.setPicture(webArticleVo.getPicture());
        webArticle.setTypeId(webArticleVo.getTypeId());
        webArticle.setContent(webArticleVo.getContent());
        webArticle.setSort(webArticleVo.getSort());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webArticle);
        }else{
            num=insert(webArticle);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-25 16:53:30
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebArticle webArticle=selectByPrimaryKey(id);
        if(webArticle!=null){
            webArticle.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webArticle.setUpdateTime(new Date());
            webArticle.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webArticle);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webArticleVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-4-25 16:53:30
     */
    public List<WebArticle> acceptList(WebArticleVo webArticleVo){
        Example example = new Example(WebArticle.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webArticleVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webArticleVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webArticleVo.getTitle())){
            criteria.andLike("title","%"+webArticleVo.getTitle()+"%");
        }
        if(StringUtil.isNotEmpty(webArticleVo.getTypeId())){
            criteria.andEqualTo("typeId",webArticleVo.getTypeId());
        }
        example.setOrderByClause(" update_time desc");
        return webArticleDao.selectByExample(example);
    }
}
